<?php
/**
 * jishupu-cms
 * @Author:技术铺
 * @Date:2021/11/21
 */

namespace App\Filters;

/**
 * 操作日志过滤器
 *
 * Class AdminLogFilter
 * @package App\Filters
 */
class AdminLogFilter extends BaseFilter
{

    /**
     * 关键词组合搜索条件
     * @param $keywords_key
     */
    protected function keywordsKey($keywords_key)
    {
        if (filled($this->filters['keywords_value'])) {
            if (filled($keywords_key)) {
                if ($keywords_key == 'created_user') {
                    return $this->builder->where('user.name', 'like', '%' . $this->filters['keywords_value'] . '%')->orWhere('user.username', $this->filters['keywords_value'])->orWhere('admin_log.admin_username', $this->filters['keywords_value']);
                } else {
                    return $this->builder->where("admin_log." . $keywords_key, 'like', '%' . str_replace('\\', '\\\\', $this->filters['keywords_value']) . '%');
                }
            } else {
                return $this->builder->where('admin_log.id', -1);
            }
        }
    }

    /**
     * 通用：状态条件
     * @param $status
     * @return mixed
     */
    protected function status($status)
    {
        if (filled($status)) return $this->builder->where('admin_log.status', $status);
    }

    /**
     * 通用：排序
     * @param $sort
     * @return mixed
     */
    protected function sort($sort)
    {
        if (filled($sort)) {
            preg_match('/(\-|\+)(.*)/', $sort, $sort_result);
            if (filled($sort_result[2]) && filled($sort_result[1])) {
                $this->builder->orderBy("admin_log." . $sort_result[2], $sort_result[1] == '-' ? 'desc' : 'asc');
                if ($sort_result[2] != 'id') {
                    $this->builder->orderBy('admin_log.id', 'desc');
                }
                return $this->builder;
            }
        }
        return $this->builder->orderBy('admin_log.id', 'desc');
    }

    /**
     * 创建时间范围
     * @param $created_at
     * @return mixed
     */
    protected function createdAt($created_at = [])
    {
        if (!empty($created_at)) {
            return $this->builder->whereBetween('admin_log.created_at', $created_at);
        }
    }


}
